package cn.qicaiwang.open.admin.controller.legaldocument;

import cn.qicaiwang.open.app.entity.LegalDocument;
import cn.qicaiwang.open.app.server.LegalDocumentService;
import cn.qicaiwang.open.common.response.ResponseResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/LegalDocument")
public class LegalDocumentController {

    @Autowired
    private LegalDocumentService legalDocumentService;

    /**
     * 上传PDF法律文书
     * @param file 上传的文件
     * @param categoryLevel1 一级分类(如:合同模板)
     * @param categoryLevel2 二级分类(如:保险合同)
     */
    @PostMapping("/upload")
    public ResponseResult<LegalDocument> uploadDocument(
            @RequestParam("file") MultipartFile file,
            @RequestParam String categoryLevel1,
            @RequestParam(required = false) String categoryLevel2) {

        // 从文件对象中获取原始文件名（不含路径）
        String documentName = file.getOriginalFilename();
        if (documentName == null || documentName.isEmpty()) {
            throw new IllegalArgumentException("文件名不能为空");
        }

        // 去除可能包含的路径信息（只保留文件名）
        documentName = documentName.substring(documentName.lastIndexOf('/') + 1);
        documentName = documentName.substring(documentName.lastIndexOf('\\') + 1);

        LegalDocument document = legalDocumentService.uploadPdfDocument(
                file, documentName, categoryLevel1, categoryLevel2);

        return ResponseResult.ok(document);
    }



    /**
     * 删除文档
     * @param id 文档ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteDocument(@PathVariable Long id) {
        try {
            boolean success = legalDocumentService.deleteDocument(id);
            if (!success) {
                return ResponseResult.error("文档不存在或已被删除");
            }
            return ResponseResult.ok();
        } catch (Exception e) {
            return ResponseResult.error("删除文档失败: " + e.getMessage());
        }
    }


    /**
     * 根据一级分类获取二级分类
     */
    @GetMapping("/categories/level2")
    public ResponseResult<List<String>> getLevel2CategoriesByLevel1(
            @RequestParam String level1) {
        List<String> categories = legalDocumentService.getLevel2CategoriesByLevel1(level1);
        return ResponseResult.ok(categories);
    }

    /**
     * 根据二级分类获取三级分类
     */
    @GetMapping("/categories/level3")
    public ResponseResult<List<String>> getLevel3CategoriesByLevel2(
            @RequestParam String level2) {
        List<String> categories = legalDocumentService.getLevel3CategoriesByLevel2(level2);
        return ResponseResult.ok(categories);
    }

    /**
     * 根据三级分类获取具体的文档模板（分页）
     * @param page
     * @param level3
     * @return
     */
    @GetMapping("/categories/page")
    public ResponseResult<Page<LegalDocument>> getDocumentsByLevel3Page(
            Page<LegalDocument> page,
            @RequestParam String level3) {

        Page<LegalDocument> pageResult = legalDocumentService.getDocumentsByLevel3(page, level3);
        return ResponseResult.ok(pageResult);
    }
}
